import java.util.ArrayDeque;
import java.util.Deque;

public class test1 {
    public int calculate(String s) {
        Deque<Integer> stack = new ArrayDeque<>();
        char[] arr = s.toCharArray();
        char op = '+';
        int i = 0,n = arr.length;
        while(i<n){
            //空格
            if(arr[i] == ' ') i++;
                //数字
            else if(  arr[i]>='0' && arr[i]<='9'){
                int num = 0;
                while(i<n && arr[i]>='0' && arr[i]<='9'){
                    num = num*10+(arr[i]-'0');
                    i++;
                }
                if(op=='+') stack.push(num);
                else if(op=='-') stack.push(-num);
                else if(op=='*'){
                    stack.push(stack.pop()*num);
                }
                else {
                    stack.push(stack.pop()/num);
                }
            }
            //符号
            else{
                op=arr[i];
                i++;
            }

        }
        //结果
        int ret = 0;
        while(!stack.isEmpty()){
            ret+=stack.pop();
        }
        return ret;
    }
}
